package com.hu.zhcc.shiro.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hu.zhcc.shiro.dao.*;
import com.hu.zhcc.shiro.entity.bo.Lixueseng520.LxsDiet;
import com.hu.zhcc.shiro.entity.bo.Lixueseng520.LxsExercise;
import com.hu.zhcc.shiro.entity.bo.Template;
import com.hu.zhcc.shiro.entity.bo.Therapy;
import com.hu.zhcc.shiro.entity.bo.TherapyLxsDiet;
import com.hu.zhcc.shiro.entity.bo.TherapyLxsExercise;
import com.hu.zhcc.shiro.entity.vo.TherapyVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TherapyServiceImpl implements TherapyService{

    @Autowired
    private TherapyMapper therapyMapper;
    @Autowired
    private TemplateMapper templateMapper;
    @Autowired
    private LxsDietDAO lxsDietDAO;
    @Autowired
    private LxsExerciseDAO lxsExerciseDAO;
    @Autowired
    private TherapyLxsDietMapper therapyLxsDietMapper;
    @Autowired
    private TherapyLxsExerciseMapper therapyLxsExerciseMapper;

    @Override
    public boolean AddTherapy(Therapy therapy) {
            return therapyMapper.insert(therapy)>0;
    }
    //分页查询
    @Override
    public PageInfo<Therapy> pageQuery(TherapyVO therapyVO) {
        PageHelper.startPage(therapyVO.getPageNum(),therapyVO.getPageSize());
        List<Therapy> byCondition = therapyMapper.findByCondition(therapyVO.getQueryString());
        PageInfo<Therapy> checkItemPageInfo = new PageInfo<>(byCondition);
        return checkItemPageInfo;
    }

    @Override
    public boolean updateTherapy(Therapy therapy,Integer[] diet,Integer[] exercises) {
        therapy.setState(0);
        therapyLxsDietMapper.deleteByTherayId(therapy.getFilenumber());
        therapyLxsExerciseMapper.deleteByTherayId(therapy.getFilenumber());
        if(diet != null && diet.length > 0){
            for (Integer diets : diet) {
                TherapyLxsDiet therapyLxsDiet = new TherapyLxsDiet();
                therapyLxsDiet.setTherapyid(therapy.getFilenumber());
                therapyLxsDiet.setDietid(diets);
                therapyLxsDietMapper.insert(therapyLxsDiet);
            }
        }
        if(exercises != null && exercises.length > 0){
            for (Integer exercisess : exercises) {
                TherapyLxsExercise therapyLxsExercise = new TherapyLxsExercise();
                therapyLxsExercise.setTherapyid(therapy.getFilenumber());
                therapyLxsExercise.setExerciseid(exercisess);
                therapyLxsExerciseMapper.insert(therapyLxsExercise);
            }
        }
        return therapyMapper.updateByPrimaryKey(therapy)>0;
    }

    @Override
    public boolean removeTherapy(Short id) {
        return therapyMapper.deleteByPrimaryKey(id)>0;
    }

    @Override
    public List<Template> find() {
        return templateMapper.show();
    }

    @Override
    public List<LxsDiet> finddietbyid(String id) {
        return lxsDietDAO.finddietbyid(id);
    }

    @Override
    public List<LxsExercise> findExercisebyid(String id) {
        return lxsExerciseDAO.findExercisebyid(id);
    }


}
